<?php


class SearchScore {
    const LOW = 1;
    const MEDIUM = 5;
    const HIGHT = 10;
}

class CourseFilterLoader extends AbstractLoader {

    protected function resolveObject($className) {
        $realName = "By{$className}Filter";

        return new $realName();
    }

}

/**
 * Description of CourseFilter
 *
 * @author JeyDotC
 */
interface CourseFilter {

    public function filter(Cursos $course, $params);
}

class ByNameFilter implements CourseFilter {

    public function filter(Cursos $course, $name) {
        $score = SearchScore::LOW;
        
        if(empty($name)){
            $score = SearchScore::MEDIUM;
        }else if(strIpos($course->getMateria()->getNombreMateria(), $name) !== false){
            $score = SearchScore::HIGHT;
        }
        return $score;
    }

}

class ByDaysFilter implements CourseFilter {

    public function filter(Cursos $course, $params) {
        $score = SearchScore::LOW;
        
        if(!is_array($params) || count($params) == 0){
            $score = SearchScore::MEDIUM;
        }else if($this->hasDay($course, $params)){
            $score = SearchScore::HIGHT;
        }
        
        return $score;
    }

    private function hasDay(Cursos $curso, array $days) {
        $result = false;

        foreach ($curso->getListaHorariosCursos() as /* @var $horarioCurso HorariosCursos */$horarioCurso) {
            if (in_array($horarioCurso->getHorarioCursoPk()->getDia(), $days)) {
                $result = true;
                break;
            }
        }

        return $result;
    }

}

class ByTeacherFilter implements CourseFilter {

    public function filter(Cursos $course, $teacher) {
         $score = SearchScore::LOW;
        
        if(empty($teacher)){
            $score = SearchScore::MEDIUM;
        }else if($this->hasTeacher($course, $teacher)){
            $score = SearchScore::HIGHT;
        }
        
        return $score;
    }

    private function hasTeacher(Cursos $curso, $teacher) {
        $result = false;

        $listaHorariosCursos = $curso->getListaHorariosCursos();
        if (count($listaHorariosCursos)) {
            /* @var $docente Docentes */
            $docente = $listaHorariosCursos[0]->getDocente();

            if ($docente != null) {
                $teacherName = "{$docente->getNombre()} {$docente->getPrimerApellido()}  {$docente->getSegundoApellido()}";
                if (stripos($teacherName, $teacher)) {
                    $result = true;
                }
            }
        }

        return $result;
    }

}

?>
